package com.wskh.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wskh.edu.client.OssClient;
import com.wskh.edu.client.VodClient;
import com.wskh.edu.entity.Chapter;
import com.wskh.edu.entity.Video;
import com.wskh.edu.entity.chapter.ChapterVo;
import com.wskh.edu.entity.chapter.VideoVo;
import com.wskh.edu.mapper.ChapterMapper;
import com.wskh.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wskh.edu.service.VideoService;
import com.wskh.serviceBase.exception.XbException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.TransactionStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wskh
 * @since 2021-10-14
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private ChapterService chapterService;
    @Autowired
    private VideoService videoService;
    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    TransactionDefinition transactionDefinition;
    @Autowired
    TransactionManager transactionManager;
    @Autowired
    OssClient ossClient;


    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //最终要的到的数据列表
        List<ChapterVo> chapterVoArrayList = new ArrayList<>();
        //获取章节信息
        QueryWrapper<Chapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id", courseId);
        queryWrapper1.orderByAsc("sort", "id");
        List<Chapter> chapters = baseMapper.selectList(queryWrapper1);
        //获取课时信息
        QueryWrapper<Video> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id", courseId);
        queryWrapper2.orderByAsc("sort", "id");
        List<Video> videos = videoService.list(queryWrapper2);
        //填充章节vo数据
        int count1 = chapters.size();
        for (int i = 0; i < count1; i++) {
            Chapter chapter = chapters.get(i);
            //创建章节vo对象
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            System.out.println((chapterVo));
            //填充课时vo数据
            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();
            int count2 = videos.size();
            for (int j = 0; j < count2; j++) {
                Video video = videos.get(j);
                if (chapter.getId().equals(video.getChapterId())) {
                    //创建课时vo对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVo.setFree(video.getIsFree()==1);
                    videoVoArrayList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoArrayList);
            chapterVoArrayList.add(chapterVo);
        }
        return chapterVoArrayList;
    }

    @Override
    public boolean removeChapterById(String id) {
        //根据id查询是否存在视频，如果有则提示用户尚有子节点
        if(videoService.getCountByChapterId(id)){
            throw new XbException(500,"该分章节下存在视频课程，请先删除视频课程!");
        }
        // 删除章节
        int result = baseMapper.deleteById(id);
        return result > 0;
    }

    @Override
    public boolean removeChapterByCourseId(String courseId) {

        //手动开启事务！
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);

        // 删除章节
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        boolean remove = chapterService.remove(chapterQueryWrapper);
        if(!remove){
            return false;
        }

        // 查出所有即将被删除的小节视频，将云端视频删除
        String videoSourceId;
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        List<Video> videoList = videoService.list(queryWrapper);
        for (Video video : videoList) {
            videoSourceId = video.getVideoSourceId();
            if(videoSourceId!=null){
                ossClient.deleteVideoById(videoSourceId);
            }
        }

        // 删除小节
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        boolean remove1 = videoService.remove(videoQueryWrapper);
        if(!remove1) {
            return false;
        }
        //提交
        dataSourceTransactionManager.commit(transactionStatus);
        return true;
    }


}
